<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:36
 */

namespace app\admin\model;

use think\Db;
use think\Model;
use think\Request;

class Admin extends Model
{
    /**
     * 查询首页列表数据
     */
    public function getLists()
    {
        return $this->paginate(5);
    }

    /**
     * 根据搜索条件查询首页列表数据
     */
    public function getSearchLists()
    {
        // TODO:: 搜索条件匹配
//                $search = [];
        $data = input('get.sex');
        return $this->where('sex', 'like', "%{$data}%")->paginate(5, false, [
            'query' => request()->param(),
        ]);
    }

    /**
     * 根据$id查询一条数据
     */
    public function getListById()
    {
        $data = $this->where(['id' => input('id')])->find();
        if (!empty($data)) {
            return $data->toArray();
        }
        return $data;
    }

    /**
     * 添加数据
     */
    public function add($save_path)
    {
        $data = input('post.');
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if (preg_match($pattern, $data['email'])) {
            if (strlen($data['logname']) < 6 || strlen($data['pwd']) < 6) {
                return array('error' => 1, 'msg' => '账号或密码长度小于6');
            } else {
                $data['pwd'] = md5($data['pwd']);
                $request = Request::instance();
                $data['lastip'] = $request->ip();
                $data['ctime'] = $request->time();
                $data['head'] = substr($save_path, 18);
            }
        } else {
            return array('error' => 1, 'msg' => '邮箱不正确');
        }
        return $this->save($data);
    }

    /**
     * 批量导入数据
     * @param array $datas
     *
     * @return array|false
     */
    public function addAll($datas)
    {
        return $this->allowField(true)->saveAll($datas);
    }

    /**
     * 编辑数据
     */
    public function edit($save_path)
    {
        $data = input('post.');
        $data['head'] = substr($save_path, 18);
        return $this->allowField(true)->save($data, ['id' => input('post.id')]);
    }

    /**
     * 图片编辑
     */
    public function editImage($save_path)
    {
        $data = input('post.');
        $data['head'] = $save_path;
        return $this->allowField(true)->save($data, ['id' => input('post.id')]);
    }

    /**
     * 角色修改
     */
    public function setroleupdate()
    {
        $data = input('post.');
        return $this->allowField(true)->save($data, ['id' => input('post.id')]);
    }

    /**
     * 删除数据
     * 支持批量删除和单条删除
     */
    public function del()
    {
        $ids = input('id');
        return $this->where('id', 'in', $ids)->delete();
    }

    /**
     * 修改时
     * 不修改图片默认原路径
     */
    public function getIamge()
    {
        $data = $this->where(['id' => input('id')])->find();
        return $data;
    }

    /**
     *  查询出用户拥有哪些权限
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     *
     */

    public function getRoleId($id)
    {
        $data = Db::table('zy_admin')->alias('a')->join('zy_role r', 'a.role_id=r.id')->field('r.power')->where(['r.id' => $id])->find();

        return $data;
    }

    /**
     *  查询出用户的角色
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     *
     */
    public function getRoleName()
    {
        $data = Db::table('zy_admin')->alias('a')->join('zy_role r', 'r.id=a.role_id')->field('r.role_name')->select();
        return $data;
    }

    /**
     *  获取有哪些角色
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     *
     */
    public function getRoleAdd()
    {
        $data = Db::table('zy_role')->select();
        return $data;
    }

    /**
     * 批量设置数据有效性
     */
    public function setValid()
    {
        $ids = input('post.id');
        $locking = input('post.locking');
        return $this->where('id', 'in', $ids)->update(['locking' => $locking]);
    }
}

